<script setup>
import { ref, watch, onMounted } from 'vue'
import { User, Lock, Bell, Refresh } from '@element-plus/icons-vue'
// import { userLoginService } from '@/api/user.js'
import { useUserStore } from '@/stores/index'
import { useRouter } from 'vue-router'
import axios from 'axios'
// 整个的用于提交的form数据对象
const formModel = ref({
  username: 'admin',
  password: '123456',
  code: '',
  uniqid: ''
})
// 绑定表单，获取表单内容
const form = ref()
const rules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 5, max: 10, message: '用户名必须是 5-10位 的字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符', trigger: 'blur'}
  ],
  code: [
    {required: true, message: '请输入验证码', trigger: 'blur'},
  ]
}
// 点击登录事件
const userStore = useUserStore()
const router = useRouter()
const login = async () => {
  await form.value.validate()
  //const res = await userLoginService(formModel.value)
  const res = await axios({
    url: 'http://127.0.0.1/tp5/public/index.php/login',
    method: 'POST',
    params: {
      username: formModel.value.username,
      password: formModel.value.password,
      code: formModel.value.code,
      uniqid: formModel.value.uniqid
    }
  })
  console.log('登录',res)
  userStore.setToken(res.data.data.token) // 存储token
  userStore.setUser(res.data.data) // 存储用户信息

  ElMessage.success('登录成功')
  router.push('/') // 跳转到后台首页
}
// 点击请求验证码
const codeImg = ref('')
const codeFun = async () => {
  const res = await axios({
    url: 'http://127.0.0.1/tp5/public/index.php/captcha'
  })
  console.log('验证码',res)
  codeImg.value = res.data.data.src
  formModel.value.uniqid = res.data.data.uniqid
}
// 生命周期函数
onMounted(() => codeFun())
</script>

<template>
  <!--
    校验相关
      (1) el-form => :model="ruleForm"  绑定的整个form的数据对象  { xxx，xxx，xxx }
      (2) el-form => :rules="rules"     绑定的整个rules规则对象   { xxx，xxx，xxx }
      (3) 表单元素 => v-model="ruleForm.xxx" 给表单元素，绑定form的子属性
      (4) el-form-item prop配置生效的是哪个校验规则（和rules中的字段要对应）
  -->
  <div class="login-body">
    <div class="login-wrap">
      <h2 class="h2">品优购商城后台登录</h2>
      <!--登录表单-->
      <el-form :model="formModel" :rules="rules" ref="form" size="large" autocomplete="off">
        <el-form-item prop="username">
          <el-input v-model="formModel.username" :prefix-icon="User" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="formModel.password" :prefix-icon="Lock" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="code-item" prop="code">
          <div class="code-group">
            <el-input class="code-input" v-model="formModel.code" :prefix-icon="Bell" placeholder="请输入验证码"></el-input>
            <div class="code-img" @click="codeFun">
              <img :src="codeImg" />
              <el-icon :size="20"><Refresh /></el-icon>
            </div>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button @click="login" class="button" type="primary" auto-insert-space>登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-body{
  width: 100%;
  height: 100vh;
  background-color: #f7f7f7;
  display: flex;
  align-items: center;
  justify-content: center;
  .login-wrap{
    width: 400px;
    background-color: #fff;
    border-radius: 10px;
    padding: 40px 25px 25px;
  }
  .h2{
    font-size: 30px;
    font-weight: bold;
    color: #333;
    text-align: center;
    margin-bottom: 20px;
  }
  .code-group{
    display: flex;
    .code-input{
      flex: 1;
      margin-right: 10px;
    }
    .code-img{
      width: 140px;
      display: flex;
      align-items: center;
      img{
        width: 110px;
        height: 38px;
        background-color: #eee;
      }
      .el-icon{
        margin-left: 10px;
        cursor: pointer;
      }
    }
  }
  .el-button{
    width: 100%;
  }
}
</style>
